//import * as API_Category from '../../api/category'
Component({
  properties: {
    options: {
      type: Array,
      value: []
    },
    defaultOption: {
      type: Object,
      value: {
        id: '0',
        name: '全部分类'
      }
    },
    key: {
      type: String,
      value: 'id'
    },
    text: {
      type: String,
      value: 'name'
    }
  },
  data: {
    result: [],
    isShow: false,
    current: {}
  },
  methods: {
    optionTap(e) {
      let dataset = e.target.dataset
      this.setData({
        current: dataset,
        isShow: false
      });

      // 调用父组件方法，并传参
      this.triggerEvent("change", { ...dataset })
    },
    openClose() {
      this.setData({
        isShow: !this.data.isShow
      })
    },

    // 此方法供父组件调用
    close() {
      this.setData({
        isShow: false
      })
    },
    async getCategoryList(){
      const category = {
        isShow:'Y',
        type:1
      }
      API_Category.categoryList(category,"T").then(res=>{
        let arr = []
        for(var i= 0; i < res.length; i++){
          const {id,name} = res[i]
          arr.push({id,name})
        }
        this.setData({options:arr})
          // 属性名称转换, 如果不是 { id: '', name:'' } 格式，则转为 { id: '', name:'' } 格式
          let result = []
          if (this.data.key !== 'id' || this.data.text !== 'name') {       
            for (let item of this.data.options) {
              let { [this.data.key]: id, [this.data.text]: name } = item
              result.push({ id, name })
            }
          }else{
            for (let item of this.data.options) {
              let { [this.data.key]: id, [this.data.text]: name } = item
              result.push({ id, name })
            }
          }
          this.setData({
            current: Object.assign({}, this.data.defaultOption),
            result: result
          })


      })
    }
  },
  lifetimes: {
      //列表分类
     attached() {
     
    }
  },

})